<?php
/**
 * Date: 2024.8
 * Author: kahn
 */
namespace service\app;


use config\Config;
use models\app\VersionModel;

class VersionService extends \Service{

    /*
     * 检查版本升级情况
     */
    public static function versionInfo($version, $platform): ?array
    {

        $client = VersionModel::where('version',$version)
            ->where('platform', $platform)
            ->where('status', 1)
            ->field('id, verifying, force')
            ->find();
        if(!$client){
            return null;
        }

        $max = VersionModel::where('id', function ($query) use ($platform) {
            $query->table('tiku_version')
                ->where('platform', $platform)
                ->where('status', 1)
                ->where('verifying', VersionModel::VERIFYING_NO)
                ->where('update', '<>', VersionModel::UPDATE_NO)
                ->field('max(id) id');
        })->field('id, version, info, update')
          ->find();

        $info = ['verifying'=>$client->verifying];

        if($max && $client->id < $max->id){
            $info['update'] = [
                'version' => $max->version,
                'info' => $max->info,
                'update' => $client->force == VersionModel::FORCE_YES ? VersionModel::FORCE_YES : $max->update,
                'download' => Config::APP_DOWNLOAD_URL[$platform] ?? '',
            ];
        }
        return $info;
    }


}